package com.server.system.rabbitmq;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rabbitmq.client.Channel;
import com.server.model.all.system.bo.AddLogOperationBo;
import com.server.model.all.system.bo.AddSmsLogOperationBo;
import com.server.model.all.enums.SystemEnum;
import com.server.common.code.utils.GsonUtils;
import com.server.commons.rabbitmq.constant.LogRabbitMQConstant;
import com.server.system.service.SmsOperationLogService;
import com.server.system.service.UserOperationLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Slf4j
@Configuration
public class SystemRabbitMQ {

    @Resource
    private UserOperationLogService userOperationLogService;

    @Resource
    private SmsOperationLogService smsOperationLogService;



    /**
     * 接受添加日志操作
     * @author LXC
     * @date 2022/6/19 18:41
     */
    @RabbitListener(
            bindings = {
                    @QueueBinding(value = @Queue(value = LogRabbitMQConstant.USER_OPERATION_LOG_QUEUE),
                    exchange = @Exchange(value = LogRabbitMQConstant.LOG_EXCHANGE),
                    key = {LogRabbitMQConstant.USER_OPERATION_LOG_ROUTING_KEY})
            },
            concurrency = "2",ackMode = "MANUAL"
    )
    @RabbitHandler
    public void addLogMessage(Channel channel, Message message){
        String str = StrUtil.str(message.getBody(), StandardCharsets.UTF_8);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            AddLogOperationBo addLogOperationBo = GsonUtils.getGson().fromJson(str, AddLogOperationBo.class);
            userOperationLogService.addLogOperation(addLogOperationBo);

            //消费成功
            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            log.error("添加日志失败",e);
        }
    }

    /**
     * 接受添加短信信息
     * @author LXC
     * @date 2022/6/19 18:41
     */
    @RabbitListener(
            bindings = {@QueueBinding(
                    value = @Queue(value = LogRabbitMQConstant.SMS_OPERATION_LOG_QUEUE),
                    exchange = @Exchange(value = LogRabbitMQConstant.LOG_EXCHANGE),
                    key = {LogRabbitMQConstant.SMS_OPERATION_LOG_ROUTING_KEY})
            },
            concurrency = "1",ackMode = "MANUAL"
    )
    @RabbitHandler
    public void addSmsLogMessage(Channel channel, Message message) throws IOException {
        String str = StrUtil.str(message.getBody(), StandardCharsets.UTF_8);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        String messageId = message.getMessageProperties().getMessageId();

        AddSmsLogOperationBo addSmsLogOperationBo = GsonUtils.getGson().fromJson(str, AddSmsLogOperationBo.class);
        if(ObjectUtils.isEmpty(addSmsLogOperationBo)){
            channel.basicAck(deliveryTag,false);
            return;
        }
        smsOperationLogService.addSmsLogOperation(addSmsLogOperationBo);

        channel.basicAck(deliveryTag,false);
    }
}
